package com.grace.cms.general.taglib;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.grace.redis.utils.RedisCache;
import com.grace.cms.common.domain.entity.CmsColumn;
import com.grace.cms.general.cache.GeneralColumnCacheService;
import com.grace.cms.general.utils.tree.ColumnTreeUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 菜单标签
 */
@Service("columnTag")
public class ColumnTag {
    @Autowired
    private GeneralColumnCacheService cmsColumnService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 不涉及分页（树形菜单）
     * 
     * @param columnId
     * @return
     */
    public CmsColumn treeCmsColumnById(long columnId) {
        return ColumnTreeUtils.getColumnTree(this.listCmsColumnBy(columnId), columnId);
    }

    /**
     * 通过id获取导航信息
     * 
     * @param columnId
     * @return
     */
    public CmsColumn selectCmsColumnById(long columnId) {
        return cmsColumnService.selectCmsColumnById(columnId);
    }

    /**
     * 通过id获取导航列表
     * 
     * @param columnId
     * @return
     */
    public List<CmsColumn> listCmsColumnById(long columnId) {
        return this.listCmsColumnBy(columnId);
    }

    /**
     * 获取所有菜单列表
     * 
     * @return
     */
    public List<CmsColumn> listCmsColumnAll() {
        return this.listCmsColumnBy(0L);
    }

    public List<CmsColumn> listCmsColumn(CmsColumn cmsColumn) {
        return cmsColumnService.listCmsColumn(cmsColumn);
    }

    private List<CmsColumn> listCmsColumnBy(long columnId) {

        // 尝试从缓存中获取栏目数据
        List<Long> columnIds = cmsColumnService.listCmsColumnIdsById(columnId);
        ArrayList<Object> categoryIdObjects = new ArrayList<>(columnIds.stream()
                .map(Object::toString)
                .collect(Collectors.toList()));
        List<CmsColumn> columns = redisCache.getMultiCacheMapValue(GeneralColumnCacheService.allKeyPrefix, categoryIdObjects);
        if (columns.stream().anyMatch(column -> column == null)) {
            return filterListNull(columns, columnIds,columnId);
        }
        else {
            return columns;
        }

    }

    private List<CmsColumn> filterListNull(List<CmsColumn> columns, List<Long> columnIds,long columnId) {
        // 创建一个新的列表用于保存非空的 CmsColumn 项
        List<CmsColumn> nonNullColumns = new ArrayList<>();
        for (int i = 0; i < columnIds.size(); i++) {
            if (columns.get(i) != null) {
                nonNullColumns.add(columns.get(i));
            } else {
                CmsColumn cmsColumn = cmsColumnService.selectCmsColumnById(columnIds.get(i));
                if (cmsColumn != null) {
                    redisCache.setCacheMapValue(GeneralColumnCacheService.allKeyPrefix,
                           Long.toString(cmsColumn.getColumnId()), cmsColumn);
                    nonNullColumns.add(cmsColumn);
                } else {

                }
            }
        }
        return nonNullColumns;
    }

}
